//
//  SiginCellCollection.m
//  签到
//
//  Created by Burn on 2018/12/13.
//  Copyright © 2018 nmgxysh. All rights reserved.
//

#import "SiginCellCollection.h"
#import "DayModel.h"
#import "NSString+Base.h"
@interface SiginCellCollection()

@property(strong,nonatomic) UILabel *topLabel;

@property(strong,nonatomic) UILabel *bottomLabel;

@property(strong,nonatomic) CAShapeLayer *cashapeLayer;

@property(strong,nonatomic) UIBezierPath *bzPath;


@end

CGFloat spacing = 2.0f;

@implementation SiginCellCollection

-(instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        
        
        
        self.topLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height * 0.5)];
        self.topLabel.textAlignment = NSTextAlignmentCenter;
        self.topLabel.font = [UIFont systemFontOfSize:14];
//        self.topLabel.backgroundColor = [UIColor clearColor];
        self.topLabel.numberOfLines = 2;
        [self.contentView addSubview:self.topLabel];
        
        self.bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.bounds.size.height * 0.5, self.bounds.size.width, self.bounds.size.height * 0.5)];
        self.bottomLabel.textColor = [UIColor lightGrayColor];
//        self.bottomLabel.backgroundColor = [UIColor clearColor];
        self.bottomLabel.textAlignment = NSTextAlignmentCenter;
        self.bottomLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.bottomLabel];
        
    }
    return self;
}


- (void)setModel:(DayModel *)model{
    self.topLabel.text = [NSString stringWithFormat:@"%ld",model.day];
    self.bottomLabel.text = model.lunar;
    self.bottomLabel.textColor = [model.lunar isEqualToString:@"今天"] ? [UIColor redColor] : [UIColor lightGrayColor];
    
    [self.cashapeLayer removeFromSuperlayer];
    if (model.sigin) {
        [self.contentView.layer insertSublayer:self.cashapeLayer atIndex:0];
    }
}

- (void)sigin{
    
    if (self.model.isSigin) { return ;}
    
    self.cashapeLayer.path = self.bzPath.CGPath;
    [self.contentView.layer insertSublayer:self.cashapeLayer atIndex:0];
    
    CABasicAnimation *base = [CABasicAnimation animation];
    base.keyPath = @"transform.scale";
    base.toValue = [NSNumber numberWithFloat:1.2];
    [self.cashapeLayer addAnimation:base forKey:nil];
}

- (CAShapeLayer *)cashapeLayer{
    if (!_cashapeLayer) {
        _cashapeLayer = [CAShapeLayer layer];
        _cashapeLayer.frame = self.bounds;
        _cashapeLayer.lineWidth = 5;
        _cashapeLayer.fillColor = [UIColor orangeColor].CGColor;
        _cashapeLayer.strokeEnd = 0;
    }
    return _cashapeLayer;
}

- (UIBezierPath *)bzPath{
    if (!_bzPath) {
        _bzPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(spacing, spacing, self.bounds.size.width - spacing * 2, self.bounds.size.width - spacing * 2) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(self.bounds.size.width - spacing * 2, self.bounds.size.width - spacing * 2)];
        
    }
    return _bzPath;
}

@end
